package system;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/reloadTestInf")
public class reloadTestInf extends HttpServlet {
	private static final long serialVersionUID = 1L;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	int type=Integer.parseInt(req.getParameter("type"));
    	int partNo=Integer.parseInt(req.getParameter("partNo"));
    	int questionNo=Integer.parseInt(req.getParameter("questionNo"));
		String testid=req.getParameter("testid");
		
		String result = "";
		
		sqlOperation sql = new sqlOperation();
		
		if(type==1) {
			HttpSession session=req.getSession();
			session.setAttribute("testid",testid);
			String[] inf=sql.getQueryLine("select * from testing where test_id='"+testid+"'",6);
			result="{\"partNum\":\""+inf[3]+"\",\"testName\":\""+inf[2]+"\",\"totalPoint\":\""+inf[4]+"\"}";
		}else if(type==2) {
			String[] inf=sql.getQueryLine("select * from parts where test_id='"+testid+"' and part_no='"+partNo+"'",6);
			result="{\"partName\":\""+inf[1]+"\",\"typeList\":\""+inf[5]+"\",\"questionNum\":\""+inf[3]+"\",\"perPoint\":\""+inf[4]+"\"}";
		}else if(type==3) {
			String questionId = sql.getQuerySet("select question_id from questions where test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"'",1)[0];
			result="{\"questionId\":\""+questionId+"\"}";
		}
		resp.setContentType("application/json;charset=UTF-8");
		PrintWriter out = resp.getWriter();		
		out.write(result);	
    }   
    

}
